package leetcode.editor.cn;

//请完成一个函数，输入一个二叉树，该函数输出它的镜像。 
//
// 例如输入： 
//
// 4 
// / \ 
// 2 7 
// / \ / \ 
//1 3 6 9 
//镜像输出： 
//
// 4 
// / \ 
// 7 2 
// / \ / \ 
//9 6 3 1 
//
// 
//
// 示例 1： 
//
// 输入：root = [4,2,7,1,3,6,9]
//输出：[4,7,2,9,6,3,1]
// 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 1000 
//
// 注意：本题与主站 226 题相同：https://leetcode-cn.com/problems/invert-binary-tree/ 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 195 👎 0

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class ErChaShuDeJingXiangLcof {
    public static void main(String[] args) {
//        Solution solution = new ErChaShuDeJingXiangLcof().new Solution();
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);
        new ErChaShuDeJingXiangLcof().new Solution().mirrorTree(root);

    }
//leetcode submit region begin(Prohibit modification and deletion)

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }


     class Solution {
        public TreeNode mirrorTree(TreeNode root) {
            if (root == null) return null;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                System.out.println(node.val);
                if (node.left != null) stack.push(node.left);
                if (node.right != null) stack.push(node.right);
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
            }
            return root;
//            TreeNode tmp = root.left;
//            root.left = mirrorTree(root.right);
//            root.right = mirrorTree(tmp);
//            return root;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
